<?php
/**
 * @File	  : login.php
 * @Project	  : vncphathoc
 * @author	  : ThanhMinh
 * @email	  : thanhminhld@yahoo.com - thanhminhld@gmail.com
 * @copyright : 2009
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Base_Controller
{
	/**
	 * Constructor function
	 */
	public function __construct()
	{
		parent::Controller();
		//parent::__construct();
//		$langDefault         = $this->lang_model->getLangDefault();
//		$this->lang_default  = $langDefault->id;
//		$lang_tag            = array('lang_tag' => $langDefault->tag);
//		$this->lang_tag      = $langDefault->tag;
//		$this->session->set_userdata($lang_tag);
		//var_dump($this->session);die;
	}
	/**
	  * _remap
	  * @access public
	  * @param
	  * @return
	  */
	public function _remap( $method )
	{
		switch ($method) {

			case 'logout':
				$this->logout();
				break;

			case 'forgotpwd':
			    $this->forgotPassword();
			    break;

			case 'pwdReset':
			    $this->reSetPassword();
			    break;

			default:
				$this->index();
		}
	}
	/**
	 * index : verify user login
	 *
	 * @return : move to main or login form
	 */
	public function index()
	{
		// Submit button is pressed
		if ($_SERVER['REQUEST_METHOD']==='POST') {
			$username = $this->input->post('user_name');
			$password = $this->input->post('pass_word');
			$lang	  = $this->input->post('admLang');
			$user = $this->users_model->checkLogin( $username, $password );

			if (is_object($user)) {
				$session_data = array( 'adm_login_id' => $user->user_rule);
				$this->session->set_userdata($session_data);

				header('Location:index.php?c=main');
			} else {
				header('Location:index.php?c=login');
			}
		}
        $controlName    = $this->input->get_post('c');
		$this->msmarty->assign('forgotpwd_link', '?c=' . $controlName . '&m=forgotpwd');

		$this->msmarty->display('login/admin.login.tpl');
	}
	/**
	 * forgotPassword
	 *
	 * @author	: Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 */
	private function forgotPassword()
	{
	    $controlName    = $this->input->get_post('c');
        /**
         * Get message when save insert or edit submit
         *
         */
        $msgBoxDisp = '';
        $clsType    = '';
        $clsIcon    = '';

        $messages = $this->getErrMsg($msgBoxDisp, $clsType, $clsIcon);

        $this->msmarty->assign('messages', $messages);
        $this->msmarty->assign('clsType', $clsType);
        $this->msmarty->assign('clsIcon', $clsIcon);
        $this->msmarty->assign('msgBoxDisp', $msgBoxDisp);
	    $this->msmarty->assign('actionForm', '?c=' . $controlName . '&m=pwdReset');
	    $this->msmarty->assign('login_page_link', '?c=' . $controlName);
	    $this->msmarty->display($controlName . '/admin.forgotpwd.tpl');
	}
	/**
	 * reSetPassword
	 *
	 * @access	: private
	 * @author	: Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 *
	 */
	private function reSetPassword()
	{
	    if ($_SERVER['REQUEST_METHOD']==='POST') {
	        $email     = $this->input->post('email');
            //Load lang msg
            $this->lang->load('success', $this->lang_tag);
            $this->lang->load('error', $this->lang_tag);
            $this->load->library('email');
            $errMsg = array();
            if (!$this->email->valid_email($email)) {
                array_push($errMsg, $this->lang->line('setting_email_invalid'));
            }
            /*
	    	if (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) {
                array_push($errMsg, $this->lang->line('setting_email_invalid'));
	        }*/
	    	if (count($errMsg) > 0) {
                foreach ($errMsg as $k => $msg) {
                    $this->messages->add($msg, 'error');
                }
                $this->forgotPassword();
                return FALSE;
            }
            $this->email->from('thanhminhld@yahoo.com');
            $this->email->to($email);
            $this->email->subject('Gui lai mat khau');
            $body = 'Mat khau moi : ' . dohash($email, 'md5');
            $this->email->message($body);
            $ret = $this->email->send();
            if ($ret == FALSE) {
                /*$msg = $this->email->print_debugger();
                $msg = substr($msg, 0, strpos($msg, '<pre>'));*/
                $this->messages->add($this->lang->line('email_send_err'), 'error');
            } else {
                $this->messages->add($this->lang->line('email_send_success'), 'success');
            }
	    }
	    $this->forgotPassword();
	}
    /**
     * @function getErrMsg
     *
     * @author   Vo Thanh Minh <vothanhminh@yahoo.com - vothanhminh@gmail.com>
     * @access   private
     * @param    string $msgBoxDisp, $clsType, $clsIcon
     * @return   array
     */
    private function getErrMsg(&$msgBoxDisp, &$clsType, &$clsIcon)
    {
        $messages = $this->messages->get();

        if (!is_array($messages)) {
            $messages = array();
            $msgBoxDisp = 'none;';
        } else {
            foreach($messages as $type => $message) {
                if(!empty($message)) {
                    if ($type == 'error') {
                        $clsType = $type;
                        $clsIcon = 'errorIcon';
                    } else if ($type == 'success') {
                        $clsType = $type;
                        $clsIcon = 'successIcon';
                    }
                    break;
                }
            }
        }
        $this->messages->clear();
        return $messages;
    }
	/**
	  * logout
	  * @access public
	  * @param
	  * @return
	  */
	public function logout() {
		$this->users_model->logout();
	}
}

?>